<?php

class ProfileController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
    	$auth = Zend_Auth::getInstance();
       	if ($auth->hasIdentity()) 
        {
            $user_id = $auth->getIdentity()->id; 
        }
        else 
        {
        	$this->_redirect('authentication/login');
        }
        
        $this->getPlayerArmy($user_id);
        $this->getPlayerBuildings($user_id);
    }
	
    private function getRoundStatistics($user_id)
    {
    	$playerStats = new Application_Model_PlayerStats();
    	$playerRoundStats = $playerStats->getPlayerStats($user_id);
    	$this->view->playerStats = $playerRoundStats;
    }
    
	private function getPlayerArmy($user_id)
    {
    	$playerProfile = new Application_Model_PlayerProfile();
    	
    	$playerInfo = $playerProfile->getPlayerArmyDetails($user_id);
    	$this->view->lvl1 = $playerInfo['lvl1'];
    	$this->view->lvl2 = $playerInfo['lvl2'];
    	$this->view->lvl3 = $playerInfo['lvl3'];
    	$this->view->lvl4 = $playerInfo['lvl4'];
    	$this->view->lvl5 = $playerInfo['lvl5'];
    	$this->view->lvl6 = $playerInfo['lvl6'];
    	
    	$totalArmy =  $playerInfo['lvl1']+ $playerInfo['lvl2']+$playerInfo['lvl3']+$playerInfo['lvl4']+ $playerInfo['lvl5']+$playerInfo['lvl6'];
    	$this->view->dis_l1 = round($playerInfo['lvl1']/$totalArmy*100);
    	$this->view->dis_l2 = round($playerInfo['lvl2']/$totalArmy*100);
    	$this->view->dis_l3 = round($playerInfo['lvl3']/$totalArmy*100);
    	$this->view->dis_l4 = round($playerInfo['lvl4']/$totalArmy*100);
    	$this->view->dis_l5 = round($playerInfo['lvl5']/$totalArmy*100);
    	$this->view->dis_l6 = round($playerInfo['lvl6']/$totalArmy*100);
    	
    	
    }
    
	private function getPlayerEquipment($user_id)
    {
    	//alpha1 no armory and armors
    }
    
	private function getPlayerBuildings($user_id)
    {
    	$buildings = new Application_Model_BuildingsTbl();
    	$userBuildings = new Application_Model_UserBuildings();
    	$result = $buildings->fetchAll();
		if($result != null)
		{
			foreach($result as $value) 
			{
				$key = $value['name'];
				$buildingsArray[$key] = 0;
			}
		}
		
		//set lvl for each building individually
		$result = $userBuildings->getUserBuildings($user_id);
		if($result != null)
		{
			foreach($result as $value) 
			{
				$key = $value['name'];
				$val = $value['building_lvl'];
				$buildingsArray[$key] = $val;
			}
		}
		
		$this->view->buildingsArray = $buildingsArray;
    }

}

